package com.wimcorp.magic.abilities;

import java.rmi.RemoteException;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.wimcorp.magic.abilities.trigger.AbilityTrigger;
import com.wimcorp.magic.cards.Creature;
import com.wimcorp.magic.exception.CastException;
import com.wimcorp.magic.input.Target;

/**
 * example {target:'THIS'}
 * 
 * target options : THIS
 * 
 * types options : 'Creature'
 * 
 * location options : BATTLEFIELD
 * 
 * behaviors options :
 * 
 * duration :
 */
@Entity
@Table(name = "regenerate", schema = "abilities")
public class Regenerate extends Ability {

	@Transient
	private Creature creature = null;

	protected Regenerate() {
	}

	public Regenerate(AbilityTrigger trigger) {
		super(trigger);
	}

	public Regenerate(Regenerate ability) {
		super(ability);
	}

	@Override
	protected void executeAbility() throws CastException {
		creature = (Creature) getAbilityTarget();
		if (creature != null) {
			if (canRegenerate(creature)) {
				creature.cast();
			}else{
				throw new CastException(String.format("Creature %s's toughness is 0 or less and can't be regenerated.", creature.getName()));
			}
		}
	}

	private boolean canRegenerate(Creature creature) {
		creature.resolveCreature();
		if (creature.getActualToughness() > 0) {
			return true;
		}
		return false;
	}

	@Override
	public Regenerate clone() {
		return new Regenerate(this);
	}

	@Override
	public boolean isInputRequired() {
		if (Target.THIS.equals(getTarget())) {
			return false;
		}
		return true;
	}

}
